{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "BOT-nn.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "metadata": {
        "id": "Y9HKwR_mPaO3",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 142
        },
        "outputId": "8002ec02-421d-4e9e-a601-d790ae5126ae"
      },
      "cell_type": "code",
      "source": [
        "!pip3 install keras"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: keras in /usr/local/lib/python3.6/dist-packages (2.1.6)\n",
            "Requirement already satisfied: scipy>=0.14 in /usr/local/lib/python3.6/dist-packages (from keras) (0.19.1)\n",
            "Requirement already satisfied: six>=1.9.0 in /usr/local/lib/python3.6/dist-packages (from keras) (1.11.0)\n",
            "Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras) (2.8.0)\n",
            "Requirement already satisfied: pyyaml in /usr/local/lib/python3.6/dist-packages (from keras) (3.13)\n",
            "Requirement already satisfied: numpy>=1.9.1 in /usr/local/lib/python3.6/dist-packages (from keras) (1.14.5)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "ajca5wVnPrSl",
        "colab_type": "code",
        "colab": {
          "resources": {
            "http://localhost:8080/nbextensions/google.colab/files.js": {
              "data": "Ly8gQ29weXJpZ2h0IDIwMTcgR29vZ2xlIExMQwovLwovLyBMaWNlbnNlZCB1bmRlciB0aGUgQXBhY2hlIExpY2Vuc2UsIFZlcnNpb24gMi4wICh0aGUgIkxpY2Vuc2UiKTsKLy8geW91IG1heSBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRoZSBMaWNlbnNlLgovLyBZb3UgbWF5IG9idGFpbiBhIGNvcHkgb2YgdGhl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ICAgIH07CiAgICAgIHBlcmNlbnQudGV4dENvbnRlbnQgPQogICAgICAgICAgYCR7TWF0aC5yb3VuZCgocG9zaXRpb24gLyBmaWxlRGF0YS5ieXRlTGVuZ3RoKSAqIDEwMCl9JSBkb25lYDsKICAgIH0KICB9CgogIC8vIEFsbCBkb25lLgogIHlpZWxkIHsKICAgIHJlc3BvbnNlOiB7CiAgICAgIGFjdGlvbjogJ2NvbXBsZXRlJywKICAgIH0KICB9Owp9CgpzY29wZS5nb29nbGUgPSBzY29wZS5nb29nbGUgfHwge307CnNjb3BlLmdvb2dsZS5jb2xhYiA9IHNjb3BlLmdvb2dsZS5jb2xhYiB8fCB7fTsKc2NvcGUuZ29vZ2xlLmNvbGFiLl9maWxlcyA9IHsKICBfdXBsb2FkRmlsZXMsCiAgX3VwbG9hZEZpbGVzQ29udGludWUsCn07Cn0pKHNlbGYpOwo=",
              "ok": true,
              "headers": [
                [
                  "content-type",
                  "application/javascript"
                ]
              ],
              "status": 200,
              "status_text": ""
            }
          },
          "base_uri": "https://localhost:8080/",
          "height": 95
        },
        "outputId": "ea60c48a-aad1-4096-e57e-3220e306d533"
      },
      "cell_type": "code",
      "source": [
        "from google.colab import files\n",
        "\n",
        "uploaded = files.upload()\n",
        "\n",
        "for fn in uploaded.keys():\n",
        "  print('User uploaded file \"{name}\" with length {length} bytes'.format(\n",
        "      name=fn, length=len(uploaded[fn])))"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "\n",
              "     <input type=\"file\" id=\"files-3cf64ba1-06e5-42d0-b543-c738b8d161f3\" name=\"files[]\" multiple disabled />\n",
              "     <output id=\"result-3cf64ba1-06e5-42d0-b543-c738b8d161f3\">\n",
              "      Upload widget is only available when the cell has been executed in the\n",
              "      current browser session. Please rerun this cell to enable.\n",
              "      </output>\n",
              "      <script src=\"/nbextensions/google.colab/files.js\"></script> "
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "stream",
          "text": [
            "Saving submit.py to submit.py\n",
            "User uploaded file \"submit.py\" with length 1443 bytes\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "r7rZiCdrPt3H",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "!unzip colab_upload.zip"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "lRGo5po3UyFQ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 69
        },
        "outputId": "aae061d2-a3e2-460d-de42-d91cf460cfb1"
      },
      "cell_type": "code",
      "source": [
        "!ls"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "A-test.csv\t      data\t   hist.zip\tsample_data\r\n",
            "colab_upload (1).zip  dataset.py   __pycache__\tsubmit.py\r\n",
            "colab_upload.zip      evaluate.py  quantize.py\r\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "gMAGDXs5UB02",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4a16a2d2-ba9f-49b3-c6ed-7098d622a08a"
      },
      "cell_type": "code",
      "source": [
        "from datetime import datetime, timedelta\n",
        "\n",
        "import keras\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "from keras import backend as K\n",
        "from keras.layers import Add, Dense, Dropout, Input\n",
        "from keras.models import Model\n",
        "\n",
        "import quantize\n",
        "from dataset import get_hist_with_time\n",
        "from evaluate import deploy_model, test_model\n",
        "from submit import get_hour_density, submit_csv"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Using TensorFlow backend.\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "metadata": {
        "id": "VE7IgpZeQA07",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "is_test = True\n",
        "img_size = 58 * 99\n",
        "noise_size = 100\n",
        "\n",
        "np.random.seed(2333)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ok5dN-cjQEJW",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "def preprocess_data():\n",
        "    if is_test:\n",
        "        density, weekday, hours = get_hist_with_time(\n",
        "            datetime(2017, 2, 6), datetime(2017, 3, 5))\n",
        "    else:\n",
        "        density, weekday, hours = get_hist_with_time(\n",
        "            datetime(2017, 2, 6), datetime(2017, 3, 12))\n",
        "\n",
        "    noise_samples = np.random.uniform(size=(density.shape[0], noise_size))\n",
        "    hours = (hours.astype(\"float32\") - 8) / 14.0\n",
        "    weekday = (weekday.astype(\"float32\") + 1) / 7.0\n",
        "    #density = density.reshape((*density.shape, 1))\n",
        "    train_img = np.array([\n",
        "        img.flatten().astype(\"float32\")/100.0 for img in density])\n",
        "\n",
        "    \n",
        "    X = [noise_samples, hours, weekday]\n",
        "    y = train_img\n",
        "    return X, y\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "dI39ObjNQM6L",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "def build_model():\n",
        "    inputs_noise_img = Input(shape=(noise_size, ), name=\"noise_img\")\n",
        "    inputs_hour = Input(shape=(1, ), name=\"hour\")\n",
        "    inputs_weekday = Input(shape=(1, ), name=\"weekday\")\n",
        "\n",
        "    print(inputs_noise_img)\n",
        "\n",
        "    noise_dense = Dense(64, activation=\"relu\")(inputs_noise_img)\n",
        "    hour_dense = Dense(64, activation=\"relu\")(inputs_hour)\n",
        "    weekday_dense = Dense(64, activation=\"relu\")(inputs_weekday)\n",
        "    hour_dense_128 = Dense(128, activation=\"relu\")(inputs_hour)\n",
        "    weekday_dense_128 = Dense(128, activation=\"relu\")(inputs_weekday)\n",
        "    x = Add()([noise_dense, hour_dense])\n",
        "    x = Dense(64, activation=\"relu\")(x)\n",
        "    x = Add()([x, weekday_dense])\n",
        "    x = Dense(64, activation=\"relu\")(x)\n",
        "    x = Add()([x, hour_dense])\n",
        "    x = Dense(128, activation=\"relu\")(x)\n",
        "    x = Add()([x, weekday_dense_128])\n",
        "    x = Dense(128, activation=\"relu\")(x)\n",
        "    x = Add()([x, hour_dense_128])\n",
        "    x = Dense(256, activation=\"relu\")(x)\n",
        "    #x = keras.layers.concatenate([inputs_weekday, x])\n",
        "    x = Dropout(0.2)(x)\n",
        "    predictions = Dense(img_size)(x)\n",
        "    model = Model(inputs=[inputs_noise_img, inputs_hour, inputs_weekday],\n",
        "                  outputs=predictions)\n",
        "    return model"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "GEv1UXtzQP2r",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 2642
        },
        "outputId": "bd3515a2-ed45-48bc-a9df-fe5daaec3baf"
      },
      "cell_type": "code",
      "source": [
        "if __name__ == \"__main__\":\n",
        "    model = build_model()\n",
        "    model.compile(optimizer=\"adam\", loss='mean_squared_error')\n",
        "    for i in range(5):\n",
        "        X, y = preprocess_data()\n",
        "        model.fit(x=X, y=y, epochs=5, batch_size=7\n",
        "                  #,validation_split=0.05\n",
        "                 )\n",
        "\n",
        "    if is_test:\n",
        "        errors = []\n",
        "        date = datetime(2017, 3, 6)\n",
        "        delta_day = timedelta(days=1)\n",
        "        while date <= datetime(2017, 3, 12):\n",
        "            for hour in range(9, 23):\n",
        "                errors.append(test_model(model, date, hour))\n",
        "            date += delta_day\n",
        "        print(\"Avearage RMSE:{}\".format(np.array(errors).mean()))\n",
        "    else:\n",
        "        frames = []\n",
        "        date = datetime(2017, 3, 13)\n",
        "        delta_day = timedelta(days=1)\n",
        "        while date <= datetime(2017, 3, 26):\n",
        "            for hour in range(9, 23):\n",
        "                hist = deploy_model(model, date, hour)\n",
        "                X_sub = get_hour_density(hist, date, hour)\n",
        "                frames.append(X_sub)\n",
        "            date += delta_day\n",
        "        submission = pd.concat(frames).drop(\n",
        "            ['latitude_range', 'longitude_range'], axis=1)\n",
        "        submission.to_csv(\"./submission.csv\", index=False)"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Tensor(\"noise_img_8:0\", shape=(?, 100), dtype=float32)\n",
            "Epoch 1/5\n",
            "392/392 [==============================] - 2s 5ms/step - loss: 8.8202e-04\n",
            "Epoch 2/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 3.5025e-04\n",
            "Epoch 3/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 3.1522e-04\n",
            "Epoch 4/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 3.0198e-04\n",
            "Epoch 5/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.8847e-04\n",
            "Epoch 1/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.9982e-04\n",
            "Epoch 2/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.8490e-04\n",
            "Epoch 3/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.7866e-04\n",
            "Epoch 4/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.7253e-04\n",
            "Epoch 5/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.6881e-04\n",
            "Epoch 1/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.7483e-04\n",
            "Epoch 2/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.7337e-04\n",
            "Epoch 3/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.6263e-04\n",
            "Epoch 4/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.5733e-04\n",
            "Epoch 5/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.5120e-04\n",
            "Epoch 1/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.5422e-04\n",
            "Epoch 2/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.5711e-04\n",
            "Epoch 3/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.5193e-04\n",
            "Epoch 4/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.4772e-04\n",
            "Epoch 5/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.4504e-04\n",
            "Epoch 1/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.5117e-04\n",
            "Epoch 2/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.4860e-04\n",
            "Epoch 3/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.4482e-04\n",
            "Epoch 4/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.4375e-04\n",
            "Epoch 5/5\n",
            "392/392 [==============================] - 1s 2ms/step - loss: 2.4353e-04\n",
            "Predict 20170306 9:00 RMSE: 3.4176014981270124\n",
            "Predict 20170306 10:00 RMSE: 3.5128336140500593\n",
            "Predict 20170306 11:00 RMSE: 4.166533331199932\n",
            "Predict 20170306 12:00 RMSE: 2.792848008753788\n",
            "Predict 20170306 13:00 RMSE: 3.059411708155671\n",
            "Predict 20170306 14:00 RMSE: 3.481379037105842\n",
            "Predict 20170306 15:00 RMSE: 3.4263683398023628\n",
            "Predict 20170306 16:00 RMSE: 4.512205669071391\n",
            "Predict 20170306 17:00 RMSE: 3.286335345030997\n",
            "Predict 20170306 18:00 RMSE: 3.9344631145812006\n",
            "Predict 20170306 19:00 RMSE: 4.368065933568311\n",
            "Predict 20170306 20:00 RMSE: 4.242640687119285\n",
            "Predict 20170306 21:00 RMSE: 4.277849927241488\n",
            "Predict 20170306 22:00 RMSE: 4.551922670696417\n",
            "Predict 20170307 9:00 RMSE: 5.655086206239477\n",
            "Predict 20170307 10:00 RMSE: 5.349766350038102\n",
            "Predict 20170307 11:00 RMSE: 3.2680269276736382\n",
            "Predict 20170307 12:00 RMSE: 2.6419689627245813\n",
            "Predict 20170307 13:00 RMSE: 4.3954521951671826\n",
            "Predict 20170307 14:00 RMSE: 4.567274898667694\n",
            "Predict 20170307 15:00 RMSE: 4.24735211631906\n",
            "Predict 20170307 16:00 RMSE: 3.352610922848042\n",
            "Predict 20170307 17:00 RMSE: 3.4669871646719432\n",
            "Predict 20170307 18:00 RMSE: 4.340506882842141\n",
            "Predict 20170307 19:00 RMSE: 4.485532298401161\n",
            "Predict 20170307 20:00 RMSE: 3.6441734316577197\n",
            "Predict 20170307 21:00 RMSE: 4.106093033529562\n",
            "Predict 20170307 22:00 RMSE: 3.3286633954186478\n",
            "Predict 20170308 9:00 RMSE: 3.1400636936215163\n",
            "Predict 20170308 10:00 RMSE: 4.595650117230423\n",
            "Predict 20170308 11:00 RMSE: 3.4756294393965534\n",
            "Predict 20170308 12:00 RMSE: 4.726520919238589\n",
            "Predict 20170308 13:00 RMSE: 3.146426544510455\n",
            "Predict 20170308 14:00 RMSE: 3.4292856398964493\n",
            "Predict 20170308 15:00 RMSE: 3.255764119219941\n",
            "Predict 20170308 16:00 RMSE: 3.069201850644561\n",
            "Predict 20170308 17:00 RMSE: 4.326661530556787\n",
            "Predict 20170308 18:00 RMSE: 4.688283267892417\n",
            "Predict 20170308 19:00 RMSE: 3.7094473981982814\n",
            "Predict 20170308 20:00 RMSE: 3.8392707640904935\n",
            "Predict 20170308 21:00 RMSE: 3.2984845004941286\n",
            "Predict 20170308 22:00 RMSE: 5.2687759489277965\n",
            "Predict 20170309 9:00 RMSE: 4.853864439804639\n",
            "Predict 20170309 10:00 RMSE: 3.22490309931942\n",
            "Predict 20170309 11:00 RMSE: 3.746998799039039\n",
            "Predict 20170309 12:00 RMSE: 3.2893768406797053\n",
            "Predict 20170309 13:00 RMSE: 3.3075670817082456\n",
            "Predict 20170309 14:00 RMSE: 3.069201850644561\n",
            "Predict 20170309 15:00 RMSE: 4.985980344927164\n",
            "Predict 20170309 16:00 RMSE: 5.332916650389353\n",
            "Predict 20170309 17:00 RMSE: 4.404543109109048\n",
            "Predict 20170309 18:00 RMSE: 3.5071355833500366\n",
            "Predict 20170309 19:00 RMSE: 3.2093613071762426\n",
            "Predict 20170309 20:00 RMSE: 3.3823069050575527\n",
            "Predict 20170309 21:00 RMSE: 4.039801975344831\n",
            "Predict 20170309 22:00 RMSE: 5.47905101272109\n",
            "Predict 20170310 9:00 RMSE: 4.317406628984581\n",
            "Predict 20170310 10:00 RMSE: 3.6932370625238775\n",
            "Predict 20170310 11:00 RMSE: 3.1336879231984796\n",
            "Predict 20170310 12:00 RMSE: 4.404543109109048\n",
            "Predict 20170310 13:00 RMSE: 2.898275349237888\n",
            "Predict 20170310 14:00 RMSE: 3.7202150475476548\n",
            "Predict 20170310 15:00 RMSE: 2.8284271247461903\n",
            "Predict 20170310 16:00 RMSE: 3.7336309405188937\n",
            "Predict 20170310 17:00 RMSE: 3.560898762952971\n",
            "Predict 20170310 18:00 RMSE: 4.09878030638384\n",
            "Predict 20170310 19:00 RMSE: 3.4669871646719432\n",
            "Predict 20170310 20:00 RMSE: 3.8026306683663087\n",
            "Predict 20170310 21:00 RMSE: 4.608687448721165\n",
            "Predict 20170310 22:00 RMSE: 3.994996871087636\n",
            "Predict 20170311 9:00 RMSE: 3.3763886032268267\n",
            "Predict 20170311 10:00 RMSE: 4.059556626036888\n",
            "Predict 20170311 11:00 RMSE: 4.536518488885502\n",
            "Predict 20170311 12:00 RMSE: 5.180733538795447\n",
            "Predict 20170311 13:00 RMSE: 3.215587038162705\n",
            "Predict 20170311 14:00 RMSE: 3.3075670817082456\n",
            "Predict 20170311 15:00 RMSE: 3.8755644750152203\n",
            "Predict 20170311 16:00 RMSE: 3.6138621999185303\n",
            "Predict 20170311 17:00 RMSE: 3.420526275297414\n",
            "Predict 20170311 18:00 RMSE: 4.156921938165306\n",
            "Predict 20170311 19:00 RMSE: 4.787483681434329\n",
            "Predict 20170311 20:00 RMSE: 4.55411901469428\n",
            "Predict 20170311 21:00 RMSE: 4.315089802078283\n",
            "Predict 20170311 22:00 RMSE: 3.844476557348217\n",
            "Predict 20170312 9:00 RMSE: 3.3852621759621515\n",
            "Predict 20170312 10:00 RMSE: 3.5972211497209896\n",
            "Predict 20170312 11:00 RMSE: 3.671511950137164\n",
            "Predict 20170312 12:00 RMSE: 3.736308338453881\n",
            "Predict 20170312 13:00 RMSE: 2.9832867780352594\n",
            "Predict 20170312 14:00 RMSE: 3.4871191548325386\n",
            "Predict 20170312 15:00 RMSE: 3.666060555964672\n",
            "Predict 20170312 16:00 RMSE: 3.7841775856849003\n",
            "Predict 20170312 17:00 RMSE: 2.9017236257093817\n",
            "Predict 20170312 18:00 RMSE: 2.8495613697550013\n",
            "Predict 20170312 19:00 RMSE: 3.7709415269929605\n",
            "Predict 20170312 20:00 RMSE: 3.714835124201342\n",
            "Predict 20170312 21:00 RMSE: 3.6138621999185303\n",
            "Predict 20170312 22:00 RMSE: 2.130727575266252\n",
            "Avearage RMSE:3.8314472167688236\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}